This page last changed on Feb 05, 2010 by brian.

EJB 3

Notes from https://www.sun.com/offers/details/java_ee6_javabeans.xml

  • EJB allows 'interface-less' session beans.
  • EJB's can now be packaged in WAR files. EAR's are not required
  • Profiles
    • Web profile: Local Session Beans, CMT/MBT, Declarative Security, Interceptors
    • Full profile: Lite + Message-Driven Beans, Timer Servie, Async method calls, CMP/BMP Entity Beans
  • Portable JNDI names:
    • Globally unique name:
      java:global[/<app-name>]/<module-name>/<ejb-name>
    • Unique name within same application:
      java:app/<module-name>/<ejb-name>
    • Unique name within defining module:
      java:module/<ejb-name>
    • Example:
      @Stateless
      public class HelloBean implements Hello {
          public String sayHello(String msg) {
              return "Hello, " + msg;
          }
      }
      If deployed as hello.jar, JNDI entries are:
      java:global/hello/HelloBean
      java:app/hello/HelloBean
      java:module/HelloBean
  • Embeddable EJBContainer for testing or JSE use:
    EJBContainer c = EJBContainer.createEJBContainer();
    Bank bank = (Bank) c.getContext().lookup("java:global/bank/BankBean");
    // ...
    c.close();
  • New Features
    • Singletongs
      • Allows shared state
      • Designed for concurrent access
      • Lots in common with stateless/stateful beans
      • Example:
        // EJB
        @Singleton
        // @Startup // optionally create this bean eagerly
        public class SharedBean {
          private SharedData shared;
          
          // Note we can do things during construction and teardown (optional)
          @PostConstruct
          private void init() {
            shared = ...;
          }
        
          @PreDestroy
          private void destroy() {
            ...
          }
        
          public int getXYZ();
            return shared.xyz;
          }
        
          
        } 
        
        // Client
        @Stateless
        public class FooBean {
          // Inject reference to Singleton bean 
          @EJB
          private SharedBean shared
        
          public void foo() {
            int xyz = shared.getXYZ();
            ...
          }
        }
    • Concurrecy options: Single threaded (default), Container Managed (via method-level locking metadata), Bean Managed (all concurrent invocation can access bean instance)
  • Improvements to EJB timer service.
    • richer calendar-base way to express timer expressions (like cron, but better)
    • Automatic Timer Creation:
      @Stateless
      public class BankBean {
      
        @PersistenceContext EntityManager accountDB;
        @Resource javax.mail.Session mailSession;
        
        // Callback the last day of each month at 8 a.m.
        @Schedule(hour="8", dayOfMonth="Last")
        void sendMonthlyBankStatements() {
           ...
        }
      }
  • Asynchrounouse Session Bean Invocations
    • Fire and Forget or async results via Future<V>
    • Available for Stateful, Stateless and Singleton beans
    • Example of Fire and Forget:
      @Stateless public class DocBean {
        
        @PersistenceContext EntityManager resultsDB;
        @EJB DocBean myself;
      
        public void processDocument(Document doc) {
          myself.doAnalysisA(document);
          myself.doAnalysisB(document);
        }
      
        @Asynchronous public void doAnalysisA(Document d) { //... }
        @Asynchronous public void doAnalysisB(Document d) { //... }
      }
    • Example of Future:
      @EJB Processor proc;
      Task task = new Task(...);
      Future<int> computeTask = processor.compute(task);
      ...
      int result = computeTask.get();
      
      // EJB
      @Stateless
      public class ProcessorBean implements Processor {
        @PersistenceContext EntityManager db;
      
        @Asynchronous
        public Future<int> compute(Task t) {
          // perform computation
          int result = ...;
          return new javax.ejb.AsynchResult<int>(result);
        }
      }
Document generated by Confluence on Feb 03, 2026 15:43